package com.itic.mall.system.inter.finance.makeMoney.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.mall.mobile.users.livingRate.entity.LivingRateResultVo;
import com.itic.mall.system.inter.finance.makeMoney.persistence.dao.FinanceUserMakeMoneyDao;
import com.itic.mall.system.inter.finance.makeMoney.persistence.model.FinanceUserMakeMoney;
import com.itic.mall.system.inter.finance.makeMoney.persistence.model.FinanceUserMakeMoneyExample;
import com.itic.mall.system.inter.finance.makeMoney.persistence.model.FinanceUserMakeMoneyExample.Criteria;
import com.itic.mall.system.inter.finance.makeMoney.persistence.model.MakeMoneyVo;


@Service
@Transactional
public class FinanceUserMakeMoneyService
     extends CrudService<FinanceUserMakeMoneyDao,FinanceUserMakeMoney,FinanceUserMakeMoneyExample> {
    @Autowired
    private FinanceUserMakeMoneyDao financeUserMakeMoneyDao;
    
    /**
     * 获取用户总的生钱
     * @param userId
     * @return
     */
    public double getUserTotalMakeMoney(String userId) {
    	Double totalMoney = this.financeUserMakeMoneyDao.getUserTotalMakeMoney(userId);
    	return totalMoney ==null?0:totalMoney.doubleValue();
    }
    /**
     * 获取用户今日生钱
     * @param userId
     * @return
     */
    public double getUserTodayMakeMoney(String userId) {
    	FinanceUserMakeMoney financeUserMakeMoney = this.getUserTodayFinanceUserMakeMoney(userId);
    	return financeUserMakeMoney ==null?0:financeUserMakeMoney.getAmount();
    }
    
    /**
     * 获取用户今日生钱记录
     * @param userId
     * @return
     */
    public FinanceUserMakeMoney getUserTodayFinanceUserMakeMoney(String userId) {
    	FinanceUserMakeMoneyExample example = new FinanceUserMakeMoneyExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	example.setOrderByClause(" CREATE_TIME desc");
    	List<FinanceUserMakeMoney>  financeUserMakeMoneys = this.list(example);
    	if(!financeUserMakeMoneys.isEmpty())
    		return financeUserMakeMoneys.get(0);
    	else
    		return null;
    }
    /**
     * 分页查看用户最近的生钱记录 
     * @param userId
     * @param example
     * @return
     */
    public List<FinanceUserMakeMoney> getUserMakeMoneyLog(String userId,FinanceUserMakeMoneyExample example){
    	Criteria criteria = example.createCriteria();
    	criteria.andUserIdEqualTo(userId);
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	example.setOrderByClause(" CREATE_TIME desc");
    	List<FinanceUserMakeMoney>  financeUserMakeMoneys = this.list(example);
    	return financeUserMakeMoneys;
    }
    /**
     * 获取昨天的生钱记录
     * @return
     */
    public List<FinanceUserMakeMoney> findUserMakeYesterday(){
    	FinanceUserMakeMoneyExample example = new FinanceUserMakeMoneyExample();
    	Criteria criteria = example.createCriteria();
    	Date date = DateHelper.addDays(DateHelper.parseDate(DateHelper.getDate()), -1);
    	criteria.andCreateTimeGreaterThan(date);
    	example.setOrderByClause(" CREATE_TIME desc");
    	List<FinanceUserMakeMoney>  financeUserMakeMoneys = this.list(example);
    	return financeUserMakeMoneys;
    }
    
    public String addFinanceUserMakeMoney(String userId,LivingRateResultVo livingRateResultVo, double amount,double consume) {
    	FinanceUserMakeMoney financeUserMakeMoney = new FinanceUserMakeMoney();
    	financeUserMakeMoney.setAmount(amount);
    	financeUserMakeMoney.setConsume(consume);
    	financeUserMakeMoney.setYearRate(livingRateResultVo.getYearRate());
    	financeUserMakeMoney.setUserId(userId);
    	financeUserMakeMoney.setType("yh");
    	financeUserMakeMoney.setInviteMarks(livingRateResultVo.getInviteMarks());
    	financeUserMakeMoney.setInviteRate(livingRateResultVo.getInviteRate());
    	financeUserMakeMoney.setConsumeMarks(livingRateResultVo.getConsumeMarks());
    	financeUserMakeMoney.setConsumeRate(livingRateResultVo.getConsumeRate());
    	financeUserMakeMoney.setShareMakrs(livingRateResultVo.getShareMarks());
    	financeUserMakeMoney.setShareRate(livingRateResultVo.getShareRate());
    	financeUserMakeMoney.setShoppingMarks(livingRateResultVo.getShoppingMarks());
    	financeUserMakeMoney.setShoppingRate(livingRateResultVo.getShoppingRate());
    	financeUserMakeMoney.setUseMarks(livingRateResultVo.getUseMarks());
    	financeUserMakeMoney.setUseRate(livingRateResultVo.getUseRate());
    	financeUserMakeMoney.setCommissionMarks(livingRateResultVo.getCommissionMarks());
    	financeUserMakeMoney.setCommissionRate(livingRateResultVo.getCommissionRate());
    	String result = this.add(financeUserMakeMoney);
    	return result;
    }
    
	public List<MakeMoneyVo> getCreateMoneyList() {
		return financeUserMakeMoneyDao.getCreateMoneyList();
	}
	
	public List<Map<String, Object>> findInviteMoneyByParams(String userId,int start,int length){
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("start", start);
		params.put("length", length);
		List<Map<String, Object>> list = this.financeUserMakeMoneyDao.findInviteMoneyByParams(params);
		return list;
	}
	
	public int findInviteMoneyCountByParams(String userId){
		Map<String, Object> params = new HashMap<>();
		Integer count  = this.financeUserMakeMoneyDao.findInviteMoneyCountByParams(userId);
		return count ==null?0:count.intValue();
	}
	
}